package com.xu.cloud.common.aop;

import com.xu.cloud.common.pojo.http.req.BaseRequest;
import com.xu.cloud.common.pojo.http.resp.BaseResponse;
import com.xu.cloud.common.process.IProcessor;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.StaticMethodMatcher;

import java.lang.reflect.Method;
import java.util.Objects;

/**
 * IProcessor业务执行接口切点
 *
 * @author xuguan
 * @see com.xu.cloud.common.process.IProcessor
 * @see com.xu.cloud.common.process.ProcessorAdvisingBeanPostProcessor
 * @since 2025/1/16
 */
public class ProcessorPointcut implements Pointcut {
    @Override
    public ClassFilter getClassFilter() {
        return new ClassFilter() {
            @Override
            public boolean matches(Class<?> cls) {
                return IProcessor.class.isAssignableFrom(cls);
            }
        };
    }

    @Override
    public MethodMatcher getMethodMatcher() {
        return new StaticMethodMatcher() {
            @Override
            public boolean matches(Method method, Class<?> targetClass) {
                return Objects.equals(method.getName(), "process")
                        && BaseResponse.class.isAssignableFrom(method.getReturnType())
                        && method.getParameterCount() == 1 && BaseRequest.class.isAssignableFrom(method.getParameterTypes()[0]);
            }
        };
    }
}
